package com.cnaaa.xinqiao.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 2018/11/19 Create by 郭文梁
 * AccessCountUtil
 * 访问量工具类
 *
 * @author 郭文梁
 * @date 2018/11/19
 */
public class AccessCountUtil {
    /**
     * 访问量生成基础
     */
    private static final int COUNT_BASE = 1000;
    private static final int COUNT_UNIT = 100;
    private static final Map<Integer, Double> randomTable = initRandomTable();

    private static Map<Integer, Double> initRandomTable() {
        Map<Integer, Double> table = new HashMap<>();
        table.put(0, -10.0);
        table.put(1, -10.0);
        table.put(2, -10.0);
        table.put(3, -10.0);
        table.put(4, -9.0);
        table.put(5, -8.0);
        table.put(6, -7.0);
        table.put(7, -7.5);
        table.put(8, -6.0);
        table.put(9, -5.0);
        table.put(10, -4.0);
        table.put(11, -3.0);
        table.put(12, -2.0);
        table.put(13, -1.0);
        table.put(14, -1.0);
        table.put(15, 1.0);
        table.put(16, 1.0);
        table.put(17, 2.0);
        table.put(18, 3.0);
        table.put(19, 4.0);
        table.put(20, 5.0);
        table.put(21, 6.0);
        table.put(22, 7.0);
        table.put(23, 2.0);
        return table;
    }

    /**
     * 生成随机访问量
     *
     * @param date 日期
     * @return 随机访问量
     */
    public static int randomCount(Date date) {
        int hour = DateUtil.getHour(date);
        Double ratio = randomTable.get(hour);
        if (ratio == null) {
            ratio = 1.0;
        }
        double random = Math.random();
        return (int) (COUNT_BASE + (COUNT_UNIT * ratio * random));
    }
}
